package com.smartcruise.util.network;


import com.smartcruise.base.BaseNormalView;
import com.smartcruise.base.RefreshContract;
import com.smartcruise.util.Key;
import com.smartcruise.util.network.baseApi.NetApiObservableFactory;
import com.smartcruise.util.network.callBack.BaseCallBack;
import com.smartcruise.util.network.customerObserver.CustomDoOnNext;
import com.smartcruise.util.network.customerObserver.SimpleBackObserver;
import com.smartcruise.util.network.customerObserver.SimpleProgressObserverImp;
import com.smartcruise.util.network.customerObserver.SimpleRefreshObserver;
import com.smartcruise.util.network.netUtils.Result;

import org.json.JSONObject;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

/**
 * Created by FengChaoQun
 * on 2018/2/2 0002
 */

public class BaseRequest {

    public static void toSubscribe(Observable<ResponseBody> o, Observer<Result> s) {
        getResultObserver(o)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s);
    }

    public static void toSubscribe(Observable<ResponseBody> o, Observer<Result> s, Consumer<Result> onNext) {
        if (onNext instanceof CustomDoOnNext) {
            toSubscribe(o, s, (CustomDoOnNext) onNext);
        } else {
            getResultObserver(o).subscribeOn(Schedulers.io())
                    .doOnNext(onNext)
                    .unsubscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(s);
        }
    }

    public static void toSubscribe(Observable<ResponseBody> o, Observer<Result> s, CustomDoOnNext doOnNext) {
        if (doOnNext.isRunInMainThread()) {
            getResultObserver(o).subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnNext(doOnNext)
                    .subscribe(s);
        } else {
            getResultObserver(o).subscribeOn(Schedulers.io())
                    .doOnNext(doOnNext)
                    .unsubscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(s);
        }
    }

    /**
     * description:将ResponseBody转化为{@link Result}
     */

    public static Observable<Result> getResultObserver(Observable<ResponseBody> observable) {
        return observable.map(new Function<ResponseBody, Result>() {
            @Override
            public Result apply(ResponseBody responseBody) throws Exception {
                JSONObject jsonObject = new JSONObject(responseBody.string());
                Result result = new Result();
                result.setCode(jsonObject.getInt(Key.code));
                result.setMessage(jsonObject.getString(Key.message));
                if (jsonObject.has(Key.data)) {
                    result.setData(jsonObject.getString(Key.data));
                }
                return result;
            }
        });
    }

    public static void normalPost(String url, Map<String, Object> map, BaseNormalView baseNormalView, BaseCallBack callBack) {
        BaseRequest.toSubscribe(NetApiObservableFactory.getInstance().normalPostObservable(url, map),
                new SimpleProgressObserverImp(baseNormalView, callBack));
    }

    public static void normalPost(String url, Map<String, Object> map, BaseNormalView baseNormalView, BaseCallBack callBack, Consumer<Result> onNext) {
        BaseRequest.toSubscribe(NetApiObservableFactory.getInstance().normalPostObservable(url, map),
                new SimpleProgressObserverImp(baseNormalView, callBack), onNext);
    }

    public static void normalPostBack(String url, Map<String, Object> map, BaseCallBack callBack) {
        BaseRequest.toSubscribe(NetApiObservableFactory.getInstance().normalPostObservable(url, map),
                new SimpleBackObserver(callBack));
    }

    public static void normalPostBack(String url, Map<String, Object> map, BaseCallBack callBack, Consumer<Result> onNext) {
        BaseRequest.toSubscribe(NetApiObservableFactory.getInstance().normalPostObservable(url, map),
                new SimpleBackObserver(callBack), onNext);
    }

    public static void normalPostRefresh(String url, Map<String, Object> map, RefreshContract.View baseView, BaseCallBack callBack, Consumer<Result> onNext) {
        BaseRequest.toSubscribe(NetApiObservableFactory.getInstance().normalPostObservable(url, map),
                new SimpleRefreshObserver(baseView, callBack), onNext);
    }

}
